﻿using System.Collections;
using System.Collections.Generic;
using Entitas;
using Entitas.CodeGeneration.Attributes;
using UnityEngine;

public class DetecteSameItemSystem : ReactiveSystem<GameEntity>
{
    public DetecteSameItemSystem(Contexts context) : base(context.game)
    {
        
    }

    protected override ICollector<GameEntity> GetTrigger(IContext<GameEntity> context)
    {
        return context.CreateCollector(GameMatcher.ComponentsDetecteSameItem);
    }

    protected override bool Filter(GameEntity entity)
    {
        return entity.hasComponentsDetecteSameItem;
    }

    protected override void Execute(List<GameEntity> entities)
    {
        foreach (GameEntity entity in entities)
        {
            if (IsMeetCondition(entity))
            {
              
                entity.isComponentsJudgeFormation = true;
                //entity.ReplaceComponentsEliminate(true);
            }
            else
            {
                entity.isComponentsJudgeFormation = false;
                //entity.ReplaceComponentsEliminate(false);
            }
        }
    }

    private bool IsMeetCondition(GameEntity entity)
    {
        int left = entity.componentsDetecteSameItem.LeftEntities.Count;
        int right = entity.componentsDetecteSameItem.RightEntities.Count;
        int up = entity.componentsDetecteSameItem.UpEntities.Count;
        int down = entity.componentsDetecteSameItem.DownEntities.Count;

        return up + down >= 2 || left + right >= 2;
    }
}
